/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


/**
 *
 * @author Carlos
 */
public class EnviarMensaje extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        SocialFP red;
        /*try{
               GestorPersistencia.crearConexion();
        }catch(ErrorConexionBD e){
                  notificarError (request, response, "Error", "Error al  conectar con la BBDD");     
                }
        red=new SocialFP();
             
                 try {
                    red.recuperaUjaenSocial();
                }catch (Exception e){
                 notificarError (request, response, "Error", "No se puede recuperar el estado de la red");
                }*/
                 //Prueba de pasar la red
        red =  (SocialFP) request.getSession ().getAttribute ("red");
        if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
        
        if (request.getParameter("mensajemuro")!=null){
            MMuro(request,response,red);
            return;}
        else
            if (request.getParameter("mensajeprivado")!=null){
              MPrivado(request,response,red);
              return;}
             else
                notificarError(request,response,"Error", "Acceso ilegal");
                return;
        
       
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    protected void notificarError (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=index.html");
        dispatcher.forward (request, response);    
    }   
     protected void MMuro (HttpServletRequest request, HttpServletResponse response, SocialFP red)
        throws ServletException, IOException
    {
        
    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
      // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>Social FP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
        //Pido el mensaje a enviar
        out.println ("<form action=\"" + response.encodeURL ("EnvioMensaje") + "\" method=\"post\">");
        out.println ("<label for=\"mensaje\" accesskey=\"M\">Mensaje: </label>");
        out.println("<input type=\"text\" name=\"mensaje\" size=40><br><br>");
        out.println("<input type=\"hidden\" name=\"Amigo\" value=\"nadie\" ><br><br>");
        out.println ("<input type=\"submit\" name=\"Aceptar\" value=\"Aceptar\" accessKey=\"T\">");
        out.println ("</form></td></tr>");
        out.println ("</table>");
        out.println ("</body>");
        out.println ("</html>");
        out.close();
                  
    }
     
     protected void MPrivado (HttpServletRequest request, HttpServletResponse response, SocialFP red)
        throws ServletException, IOException
    {
        
    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
      // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>Social FP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
        //Imprimo la lista de amigos 
        /*out.println ("<table cellspacing=\"10\" bgcolor=\"#f0f0f0\" width = \"600\"><tr><td>");
        out.println ("<table width = \"100%\">");
        out.println ("<caption>Amigos a los que puedes enviar privado</caption>");
        out.println ("<tr><th bgcolor=\"#e9e9e9\">Usuario</th><th bgcolor=\"#e9e9e9\">Email</th>");
          
                String email=  (String) request.getSession ().getAttribute ("email");
                String clave=  (String) request.getSession ().getAttribute ("clave");
                Usuario u = red.loginUsuario(email, clave);
               ArrayList lista;
               Usuario temp;
               lista= new ArrayList();
               lista=(ArrayList)u.verAmigos();
               Iterator itera = lista.listIterator();
      		  while(itera.hasNext()){
                    temp=(Usuario) itera.next();
                    out.println ("<tr>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeNombre()+ "</small></td>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeEmail() + "</small></td>");
                    out.println ("</tr>");
                    
                  }
        
        
        */
        //Pido el mensaje a enviar mostrando una lista de amigos para seleccionar uno
        out.println ("<form action=\"" + response.encodeURL ("EnvioMensaje") + "\" method=\"post\">");
        
                  //out.println ("<label for=\"Destinatario\" accesskey=\"D\">Destinatario: </label>");
                  //out.println("<input type=\"text\" name=\"email\" size=10><br><br>");
        out.println("<b><big>Selecciona el amigo</big></b><br>");

        out.println ("<select name=\"Amigo\" multiple=\"multiple\">");
        String email=  (String) request.getSession ().getAttribute ("email");
        String clave=  (String) request.getSession ().getAttribute ("clave");
        Usuario u = red.loginUsuario(email, clave);
        ArrayList lista;
        Usuario temp;
        lista= new ArrayList();
        lista=(ArrayList)u.verAmigos();
        Iterator itera = lista.listIterator();
      	while(itera.hasNext()){
            temp=(Usuario) itera.next();
            out.println ("<option value="+temp.leeEmail()+">"+temp.leeNombre()+"</option>");         
        }
       
        out.println ("</select><br>");
        out.println ("<label for=\"mensaje\" accesskey=\"M\">Mensaje: </label>");
        out.println("<input type=\"text\" name=\"mensaje\" size=40><br><br>");

        out.println ("<input type=\"submit\" name=\"Aceptar\" value=\"Aceptar\" accessKey=\"A\">");
        out.println ("</form></td></tr>");
        //out.println ("</table>");
        out.println ("</body>");
        out.println ("</html>");
        out.close();
                  
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
